{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-08-08T02:06:54.961425Z",
     "start_time": "2025-08-08T02:06:53.910206Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-07T03:21:23.116706Z",
     "start_time": "2025-08-07T03:21:23.107708Z"
    }
   },
   "cell_type": "code",
   "source": [
    "s1 = pd.Series([\"zhangsan\", \"lisi\", \"wangwu\", \"zhaoliu\", \"qianqi\"])\n",
    "s2 = pd.Series([20, 21, 22, 23, 24])\n",
    "df = pd.DataFrame({'姓名': s1, '年龄': s2})\n",
    "print(df)\n",
    "print(type(df))"
   ],
   "id": "58b85bb0bc7521e8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         姓名  年龄\n",
      "0  zhangsan  20\n",
      "1      lisi  21\n",
      "2    wangwu  22\n",
      "3   zhaoliu  23\n",
      "4    qianqi  24\n",
      "<class 'pandas.core.frame.DataFrame'>\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T02:28:28.910774Z",
     "start_time": "2025-08-08T02:28:28.888525Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df2 = pd.DataFrame(\n",
    "    {\n",
    "        \"姓名\": [\"zhangsan\", \"lisi\", \"wangwu\", \"zhaoliu\", \"qianqi\"],\n",
    "        \"年龄\": [20, 21, 22, 23, 24],\n",
    "        \"性别\": [\"男\", \"女\", \"男\", \"女\", \"男\"],\n",
    "        \"籍贯\": [\"北京\", \"上海\", \"广州\", \"深圳\", \"杭州\"],\n",
    "        \"出生年份\": [\"1994\", \"1995\", \"1996\", \"1997\", \"1998\"]\n",
    "    },\n",
    "    columns=[\"姓名\", \"性别\", \"年龄\", \"出生年份\", \"籍贯\"]\n",
    ")\n",
    "print(df2)"
   ],
   "id": "e30bd5d956e0a782",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         姓名 性别  年龄  出生年份  籍贯\n",
      "0  zhangsan  男  20  1994  北京\n",
      "1      lisi  女  21  1995  上海\n",
      "2    wangwu  男  22  1996  广州\n",
      "3   zhaoliu  女  23  1997  深圳\n",
      "4    qianqi  男  24  1998  杭州\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T05:16:09.205934Z",
     "start_time": "2025-08-08T05:16:09.197495Z"
    }
   },
   "cell_type": "code",
   "source": "print(df2[\"姓名\"])",
   "id": "fc03f293e19d5011",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    zhangsan\n",
      "1        lisi\n",
      "2      wangwu\n",
      "3     zhaoliu\n",
      "4      qianqi\n",
      "Name: 姓名, dtype: object\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T05:17:32.181219Z",
     "start_time": "2025-08-08T05:17:32.171862Z"
    }
   },
   "cell_type": "code",
   "source": "print(df2.姓名)",
   "id": "a092f3fb0eba1086",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    zhangsan\n",
      "1        lisi\n",
      "2      wangwu\n",
      "3     zhaoliu\n",
      "4      qianqi\n",
      "Name: 姓名, dtype: object\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T05:19:31.895571Z",
     "start_time": "2025-08-08T05:19:31.887052Z"
    }
   },
   "cell_type": "code",
   "source": "print(df2.head(3))",
   "id": "2aa6ba8b7cebf806",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         姓名 性别  年龄  出生年份  籍贯\n",
      "0  zhangsan  男  20  1994  北京\n",
      "1      lisi  女  21  1995  上海\n",
      "2    wangwu  男  22  1996  广州\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T05:21:26.380086Z",
     "start_time": "2025-08-08T05:21:26.371964Z"
    }
   },
   "cell_type": "code",
   "source": "print(df2[df2.年龄 > 21])",
   "id": "46487af2f0a1ed32",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "        姓名 性别  年龄  出生年份  籍贯\n",
      "2   wangwu  男  22  1996  广州\n",
      "3  zhaoliu  女  23  1997  深圳\n",
      "4   qianqi  男  24  1998  杭州\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "b1b6a08740912b65"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T05:24:23.353174Z",
     "start_time": "2025-08-08T05:24:23.345374Z"
    }
   },
   "cell_type": "code",
   "source": "print(df2[(df2.年龄 > 21) & (df2.性别 == '男')])",
   "id": "50033997de537948",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       姓名 性别  年龄  出生年份  籍贯\n",
      "2  wangwu  男  22  1996  广州\n",
      "4  qianqi  男  24  1998  杭州\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T05:25:46.787840Z",
     "start_time": "2025-08-08T05:25:46.780956Z"
    }
   },
   "cell_type": "code",
   "source": "print(df2.sample(3))",
   "id": "31cfaed3854894f3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         姓名 性别  年龄  出生年份  籍贯\n",
      "2    wangwu  男  22  1996  广州\n",
      "0  zhangsan  男  20  1994  北京\n",
      "4    qianqi  男  24  1998  杭州\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T06:15:45.063619Z",
     "start_time": "2025-08-08T06:15:45.045371Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.DataFrame({\n",
    "    \"name\": [\"zhangsan\", \"lisi\", \"wangwu\", \"zhaoliu\", \"qianqi\"],\n",
    "    \"age\": [20, 21, 22, 23, 24],\n",
    "    \"sex\": [\"男\", \"女\", \"男\", \"女\", \"男\"],\n",
    "    \"birth\": [\"1994\", \"1995\", \"1996\", \"1997\", \"1998\"],\n",
    "    \"math_score\": [80, 90, 100, 95, 85],\n",
    "    \"english_score\": [90, 80, 95, 100, 85],\n",
    "})\n",
    "print(df.describe())"
   ],
   "id": "2ffdde307d184ef0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             age  math_score  english_score\n",
      "count   5.000000    5.000000       5.000000\n",
      "mean   22.000000   90.000000      90.000000\n",
      "std     1.581139    7.905694       7.905694\n",
      "min    20.000000   80.000000      80.000000\n",
      "25%    21.000000   85.000000      85.000000\n",
      "50%    22.000000   90.000000      90.000000\n",
      "75%    23.000000   95.000000      95.000000\n",
      "max    24.000000  100.000000     100.000000\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T06:15:49.026104Z",
     "start_time": "2025-08-08T06:15:49.014637Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.isin([20, \"zhangsan\"]))",
   "id": "be32d67a0ec2e8b6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    name    age    sex  birth  math_score  english_score\n",
      "0   True   True  False  False       False          False\n",
      "1  False  False  False  False       False          False\n",
      "2  False  False  False  False       False          False\n",
      "3  False  False  False  False       False          False\n",
      "4  False  False  False  False       False          False\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T06:43:25.593467Z",
     "start_time": "2025-08-08T06:43:25.585388Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.isna())",
   "id": "fcd981e35f69944e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    name    age    sex  birth  math_score  english_score\n",
      "0  False  False  False  False       False          False\n",
      "1  False  False  False  False       False          False\n",
      "2  False  False  False  False       False          False\n",
      "3  False  False  False  False       False          False\n",
      "4  False  False  False  False       False          False\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T06:45:01.983548Z",
     "start_time": "2025-08-08T06:45:01.976377Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.sum())",
   "id": "cfbab467f0740a18",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name             zhangsanlisiwangwuzhaoliuqianqi\n",
      "age                                          110\n",
      "sex                                        男女男女男\n",
      "birth                       19941995199619971998\n",
      "math_score                                   450\n",
      "english_score                                450\n",
      "dtype: object\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T06:46:09.702103Z",
     "start_time": "2025-08-08T06:46:09.697786Z"
    }
   },
   "cell_type": "code",
   "source": "print(df['math_score'].sum())",
   "id": "40cdde26dd6100e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "450\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T06:48:17.400309Z",
     "start_time": "2025-08-08T06:48:17.394311Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(df.math_score.max())\n",
    "print(df.math_score.min())"
   ],
   "id": "8ab9501cb30fccb5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n",
      "80\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T06:49:01.729840Z",
     "start_time": "2025-08-08T06:49:01.721467Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(df.math_score.mean())\n",
    "print(df.math_score.median())"
   ],
   "id": "ba41649ae072f7a7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "90.0\n",
      "90.0\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T06:54:25.826014Z",
     "start_time": "2025-08-08T06:54:25.807707Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.value_counts())",
   "id": "c40abc1082e8a9e6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name      age  sex  birth  math_score  english_score\n",
      "lisi      21   女    1995   90          80               1\n",
      "qianqi    24   男    1998   85          85               1\n",
      "wangwu    22   男    1996   100         95               1\n",
      "zhangsan  20   男    1994   80          90               1\n",
      "zhaoliu   23   女    1997   95          100              1\n",
      "Name: count, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T06:55:17.601017Z",
     "start_time": "2025-08-08T06:55:17.591260Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.DataFrame({\n",
    "    \"name\": [\"zhangsan\", \"zhangsan\", \"lisi\", \"wangwu\", \"zhaoliu\", \"qianqi\"],\n",
    "    \"age\": [20, 20, 21, 22, 23, 24],\n",
    "    \"sex\": [\"男\", \"男\", \"女\", \"男\", \"女\", \"男\"],\n",
    "    \"birth\": [\"1994\", \"1994\", \"1995\", \"1996\", \"1997\", \"1998\"],\n",
    "    \"math_score\": [80, 80, 90, 100, 95, 85],\n",
    "    \"english_score\": [90, 90, 80, 95, 100, 85],\n",
    "})\n",
    "print(df.value_counts())"
   ],
   "id": "d74230cbae062718",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name      age  sex  birth  math_score  english_score\n",
      "zhangsan  20   男    1994   80          90               2\n",
      "lisi      21   女    1995   90          80               1\n",
      "qianqi    24   男    1998   85          85               1\n",
      "wangwu    22   男    1996   100         95               1\n",
      "zhaoliu   23   女    1997   95          100              1\n",
      "Name: count, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T06:56:54.030501Z",
     "start_time": "2025-08-08T06:56:54.017528Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.drop_duplicates())",
   "id": "274e67b5768f78f3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       name  age sex birth  math_score  english_score\n",
      "0  zhangsan   20   男  1994          80             90\n",
      "2      lisi   21   女  1995          90             80\n",
      "3    wangwu   22   男  1996         100             95\n",
      "4   zhaoliu   23   女  1997          95            100\n",
      "5    qianqi   24   男  1998          85             85\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T06:58:31.172541Z",
     "start_time": "2025-08-08T06:58:31.166030Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.duplicated())",
   "id": "db110077d5d6294f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    False\n",
      "1     True\n",
      "2    False\n",
      "3    False\n",
      "4    False\n",
      "5    False\n",
      "dtype: bool\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T07:00:04.276113Z",
     "start_time": "2025-08-08T07:00:04.269670Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.duplicated(subset=['name', 'age']))",
   "id": "8463f9acfd303442",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    False\n",
      "1     True\n",
      "2    False\n",
      "3    False\n",
      "4    False\n",
      "5    False\n",
      "dtype: bool\n"
     ]
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T07:34:24.578464Z",
     "start_time": "2025-08-08T07:34:24.573612Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data = {\n",
    "  \"name\": [\"zhangsan\", \"lisi\", \"wangwu\", \"zhaoliu\", \"qianqi\"],\n",
    "  \"math\": [85,92,78,88,95],\n",
    "  \"english\": [90,88,85,92,80],\n",
    "  \"chinese\": [75,80,88,85,90]\n",
    "}"
   ],
   "id": "9c3591e92c5fc661",
   "outputs": [],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T07:34:58.278803Z",
     "start_time": "2025-08-08T07:34:58.264201Z"
    }
   },
   "cell_type": "code",
   "source": [
    "scores = pd.DataFrame(data)\n",
    "scores"
   ],
   "id": "66e8550d6689624a",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "       name  math  english  chinese\n",
       "0  zhangsan    85       90       75\n",
       "1      lisi    92       88       80\n",
       "2    wangwu    78       85       88\n",
       "3   zhaoliu    88       92       85\n",
       "4    qianqi    95       80       90"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>math</th>\n",
       "      <th>english</th>\n",
       "      <th>chinese</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>zhangsan</td>\n",
       "      <td>85</td>\n",
       "      <td>90</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>lisi</td>\n",
       "      <td>92</td>\n",
       "      <td>88</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>wangwu</td>\n",
       "      <td>78</td>\n",
       "      <td>85</td>\n",
       "      <td>88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>zhaoliu</td>\n",
       "      <td>88</td>\n",
       "      <td>92</td>\n",
       "      <td>85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>qianqi</td>\n",
       "      <td>95</td>\n",
       "      <td>80</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T07:39:44.340239Z",
     "start_time": "2025-08-08T07:39:44.330235Z"
    }
   },
   "cell_type": "code",
   "source": [
    "scores[\"total_score\"] = scores[[\"math\", \"english\", \"chinese\"]].sum(axis=1)\n",
    "print(scores)"
   ],
   "id": "cf549abf4460908f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       name  math  english  chinese  total_score\n",
      "0  zhangsan    85       90       75          250\n",
      "1      lisi    92       88       80          260\n",
      "2    wangwu    78       85       88          251\n",
      "3   zhaoliu    88       92       85          265\n",
      "4    qianqi    95       80       90          265\n"
     ]
    }
   ],
   "execution_count": 33
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T07:59:00.203107Z",
     "start_time": "2025-08-08T07:59:00.195106Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data = {\n",
    "  \"product_name\": [\"A\", \"B\", \"C\", \"D\"],\n",
    "  \"price\": [100,150,200,120],\n",
    "  \"sale_count\": [50, 30, 20, 40]\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "print(df)"
   ],
   "id": "47ef803a03ee1ebf",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  product_name  price  sale_count\n",
      "0            A    100          50\n",
      "1            B    150          30\n",
      "2            C    200          20\n",
      "3            D    120          40\n"
     ]
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "cdd25ed51bc47264"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
